package gui;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Doudizhu extends JFrame {
    private List<Card> pukes = new ArrayList<>();
    private List<Card> player1 = new ArrayList<>();
    private List<Card> player2 = new ArrayList<>();
    private List<Card> player3 = new ArrayList<>();
    private List<Card> base = new ArrayList<>();

    private Image image = xia.x("/images/ia_900000008.jpg");

    //   private Image bgImg= xia.x("/images/图片/3_1.jpg");
    public Doudizhu() {

        //标签组件
//        JLabel label = new JLabel(image1);
//        label.setBounds(30, 20, 110, 150);
//        label.setForeground(Color.red);
//        label.setFont(new Font("宋体",Font.PLAIN,30));
//        this.add(label);
//
//        JLabel label1=new JLabel(image1);
//        label1.setBounds(50,50,50,50);
//        this.add(label1);
        //设置空布局
        this.setLayout(null);
        this.init();
        this.xipai();
        this.fapai();
        System.out.println(player1.get(0).getPhoto());
        int y = 600;
        for (int i = 16; i >= 0; i--) {
            JLabel imageLable = new JLabel(xia.x1("/images/图片/" + player1.get(i).getPhoto()));
            imageLable.setBounds(80, y, 105, 150);
            this.add(imageLable);
            y -= 30;
        }

        int x=800;
        for (int j=16;j>=0;j--){
            JLabel imageLable = new JLabel(xia.x1("/images/图片/" + player1.get(j).getPhoto()));
            imageLable.setBounds(x, 800, 105, 150);
            x -=30;
            this.add(imageLable);

        }

        int y1=600;
        for (int k=16;k>=0;k--){
            JLabel imageLable = new JLabel(xia.x1("/images/图片/" + player1.get(k).getPhoto()));
            imageLable.setBounds(1000, y1, 105, 150);
            y1 -=30;
            this.add(imageLable);

        }

        int x2=600;
        for (int m=2;m>=0;m--){
            JLabel imageLable = new JLabel(xia.x1("/images/图片/" + player1.get(m).getPhoto()));
            imageLable.setBounds(x2, 300, 105, 150);
            x2 -=30;
            this.add(imageLable);

        }

            //设置窗口大小

        this.setSize(1200, 1000);

        //设置窗体标题名字
        this.setTitle("小夏的窗口");

        //设置窗体居中
        this.setLocationRelativeTo(null);


        //设置窗口关闭时程序结束
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        //设置窗体可见
        this.setVisible(true);

        init();
    }


    // 初始化54张扑克牌
    public void init() {
        String[] points = "3,4,5,6,7,8,9,10,J,Q,K,A,2".split(",");
        String[] types = "♠,♥,♣,♦".split(",");
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < types.length; j++) {
                pukes.add(new Card(points[i], types[j], i, j, (i + 3) + "_" + (j + 1) + ".jpg"));
            }
        }
        pukes.add(new Card("大王", "", 14, 0, "17_1.jpg"));
        pukes.add(new Card("小王", "", 13, 0, "16_1.jpg"));


    }

    // 洗牌
    public void xipai() {

        Collections.shuffle(pukes);
        System.out.println(pukes);
    }

    // 发牌
    public void fapai() {
        player1 = pukes.subList(0, 17);
        player2 = pukes.subList(17, 34);
        player3 = pukes.subList(34, 51);
        base = pukes.subList(51, 54);

        sort(player1);
        sort(player2);
        sort(player3);
        sort(base);
        System.out.println("玩家一：" + player1);
        System.out.println("玩家二：" + player2);
        System.out.println("玩家三：" + player3);
        System.out.println("底牌：" + base);
    }

    public void sort(List list) {
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getPointValue() == o2.getPointValue()) {
                    return o1.getTypeValue() - o2.getTypeValue();
                } else {
                    return o1.getPointValue() - o2.getPointValue();
                }
            }
        });
    }

    public static void main(String[] args) {
        Doudizhu doudizhu = new Doudizhu();
        doudizhu.init();
        doudizhu.xipai();
        doudizhu.fapai();
    }
}
